#include "CustomTableView.h"
#include <QLabel>
#include <QHeaderView>
#include <QDebug>
#include <QScrollBar>


#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

CustomTableView::CustomTableView(QWidget* parent)
        : QTableView(parent), resizingColumn(-1),/* resizingRow(-1),*/ initialPos(0)
{
    // 启用鼠标跟踪
    setMouseTracking(true);
    verticalHeader()->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
}

void CustomTableView::paintEvent(QPaintEvent *event)
{
    QTableView::paintEvent(event);
}

void CustomTableView::setBackgroundColor(const QString& color) {
    setStyleSheet(QString(
                      "QTableView {"
                      "   background-color: %1;"
                      "}"
                      
                      ).arg(color));
}


void CustomTableView::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton) {
        int x = event->pos().x();
        int y = event->pos().y();

        // 检查是否靠近垂直网格线
        QModelIndex index = indexAt(event->pos());
        if (index.isValid()) {
            int columnRight = columnViewportPosition(index.column()) + columnWidth(index.column());
            if (qAbs(x - columnRight) <= 5) { // 靠近阈值为5像素
                resizingColumn = index.column();
                initialPos = x;
            }
        }

        // 检查是否靠近水平网格线
        //if (index.isValid()) {
        //    int rowBottom = rowViewportPosition(index.row()) + rowHeight(index.row());
        //    if (qAbs(y - rowBottom) <= 5) { // 靠近阈值为5像素
        //        resizingRow = index.row();
        //        initialPos = y;
        //    }
        //}
    }
    QTableView::mousePressEvent(event);
}

void CustomTableView::mouseMoveEvent(QMouseEvent* event)
{
    if (resizingColumn != -1) {
        int delta = event->pos().x() - initialPos;
        int newWidth = columnWidth(resizingColumn) + delta;
        setColumnWidth(resizingColumn, qMax(newWidth, 1)); // 确保最小宽度为1
        initialPos = event->pos().x(); // 更新初始位置以避免累积误差
    }
    //else if (resizingRow != -1) {
    //    int delta = event->pos().y() - initialPos;
    //    int newHeight = rowHeight(resizingRow) + delta;
    //    setRowHeight(resizingRow, qMax(newHeight, 1)); // 确保最小高度为1
    //    initialPos = event->pos().y(); // 更新初始位置以避免累积误差
    //}
    else {
        // 检测鼠标是否靠近网格线
        if (isNearVerticalGridLine(event->pos())) {
            setCursor(Qt::SplitHCursor); // 设置水平调整光标
        }
        //else if (isNearHorizontalGridLine(event->pos())) {
        //    setCursor(Qt::SplitVCursor); // 设置垂直调整光标
        //}
        else {
            unsetCursor(); // 恢复默认光标
        }
    }
    QTableView::mouseMoveEvent(event);
}

void CustomTableView::mouseReleaseEvent(QMouseEvent* event)
{
    resizingColumn = -1;
    // resizingRow = -1;
    unsetCursor(); // 恢复默认光标
    QTableView::mouseReleaseEvent(event);
}
      // 初始位置

bool CustomTableView::isNearVerticalGridLine(const QPoint& pos) const {
    QModelIndex index = indexAt(pos);
    if (!index.isValid()) return false;

    int columnRight = columnViewportPosition(index.column()) + columnWidth(index.column());
    return qAbs(pos.x() - columnRight) <= 5; // 靠近阈值为5像素
}

bool CustomTableView::isNearHorizontalGridLine(const QPoint& pos) const {
    QModelIndex index = indexAt(pos);
    if (!index.isValid()) return false;

    int rowBottom = rowViewportPosition(index.row()) + rowHeight(index.row());
    return qAbs(pos.y() - rowBottom) <= 5; // 靠近阈值为5像素
}

